#!/usr/bin/env python

import flickrlib
import sys
from optparse import OptionParser

parser = OptionParser()
parser.add_option("-k", "--api-key", dest="api_key", action="store", 
					type="string", help="Flickr API key", metavar="KEY")
parser.add_option("-s", "--shared-secret", dest="ssecret", action="store", 
					type="string", help="Flickr API key's shared secret", metavar="SECRET")

(options, args) = parser.parse_args()

if not options.api_key or not options.ssecret:
	print 'Must provide API key and shared secret'
	parser.exit()

agent = flickrlib.FlickrAgent(options.api_key, options.ssecret)
url = agent.authLoginURL('delete')
print '''Authorizing is a simple process which takes place in your web browser. 
When you're finished, return to this terminal to complete authorization and begin developing with this library'''
print 'Go to: %s' % url
print 'Once done, press Enter.',
sys.stdin.readline()
agent.retrieveToken()
try:
	agent.flickr.test.login()
	print 'Token works! Saving it...'
	agent.saveToken()
	print 'Next time you create an instance of FlickrAgent with your API key this token will be loaded from disk'
	print '...Don\'t forget to create an aquivalent interface for your app\'s users'
	print 'Read more about it at: http://www.flickr.com/services/api/misc.userauth.html'
except:
	raise
